<?php
/***
*
* Class: MessageController //消息控制器
* Author: Jason ZHENG
* Data: 2018-06-26
*
***/
namespace App\Http\Controllers;

use App\Service\DataReturnService as DRS;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Session; 
use App\Common\Base;
use App\Validator\OrderValidator;
use App\Service\OrderService;

class MessageController extends Controller
{
    /**
     * 实例化一个新的控制器实例。
     *
     * @return void
     */
    public function __construct()
    {
        
    }

    //小程序公众号消息转发验证服务器地址
    public function check_signature(Request $request)
	{
	    $signature 	= $request->get("signature");
	    $timestamp 	= $request->get("timestamp");
	    $nonce 		= $request->get("nonce");

	    $token 	= 'growthupup';
	    $tmpArr = array($token, $timestamp, $nonce);
	    sort($tmpArr, SORT_STRING);
	    $tmpStr = implode( $tmpArr );
	    $tmpStr = sha1( $tmpStr );

	    if( $tmpStr == $signature ){
	        return $request->get("echostr");
	    }else{
	        return 0;
	    }
	}
	/**
	 * 刘家腾
	 * 2018-8-2
	 * 提醒发货
	 *
	 * @param      \Illuminate\Http\Request  $request  The request
	 *
	 * @return     <type>                    ( description_of_the_return_value )
	 */
	public function message_order(Request $request)
	{
	    $postData  = $request->post();
	    $validator = Validator::make($postData, 
                                    [
                                        'token'         => 'required|string',
                                        'batchcode'   => 'required|string', 
                                        'business_id'   => 'required',
                                    ]);
        if ($validator->fails()) {
            return DRS::returnError(3000,'参数错误！',$validator->errors());
        }
        $userInfo = Base::getUserByTokenWithPhone($postData['token']);
        if($userInfo['code'] != '666'){
            return $userInfo;
            exit;
        }

        $time = date("Y-m-d H:i:s", time());
        $now_time = strtotime($time);
        $update_time = DB::table('message_order')
                ->select('update_time','create_time','count')
                ->where(['batchcode'=>$postData['batchcode'],'type'=>0])
                ->orderBy('id','desc')
                ->get()->toarray();
        $update_time_arr = array_map('get_object_vars', $update_time); 

        if(empty($update_time_arr['0'])){

        	$data['user_id']=$userInfo['data']['id'];
	        $data['business_id']=$postData['business_id'];
	        $data['batchcode']=$postData['batchcode'];
	        $data['content']='等的花儿都谢了 快点给我发货吧！';
	        $data['create_time']=$time;
		    $message_order_id = DB::table('message_order')->insertGetId($data);
		    return DRS::returnOk(666,'提醒成功！',$message_order_id);
        }
        if(!empty($update_time_arr['0']['update_time'])){
            $update_times = strtotime( $update_time_arr['0']['update_time'] );
            $difference=$now_time-$update_times;
        }else{
            $create_times = strtotime($update_time_arr['0']['create_time']);
            $difference=$now_time-$create_times;
        }
    	
    	
        if($difference < 300){
        	return DRS::returnError(3600,'已经提醒过了 5分钟后才能再次提醒呢！',$difference);
        }
        $count = $update_time_arr['0']['count']+1;
        DB::table('message_order')->where('batchcode', $postData['batchcode'])->update(['update_time' => $time,'count'=>$count]);
		return DRS::returnOk(666,'提醒成功！',$count);
	}

    // 获取系统消息 --- type = 2
    public function message_system(Request $request)
    {
        $postData  = ['token' => $request->get('token')];
        $validator = Validator::make($postData, 
                                    [
                                        'token'          => 'required|string',
                                    ],
                                    [
                                        'token.required' => 'token获取失败'
                                    ]
                                );

        if ($validator->fails()) {
            return DRS::returnError(1000,'验证错误！',$validator->errors()->first());
        }

        $userInfo = Base::getUserByToken($postData['token']);
        if($userInfo['code'] != '666'){
            return $userInfo;
        }
        $user   = $userInfo['data'];

        $systemMessages = DB::table('message')
                        ->where(['business_id' => $user['business_id'], 'state' => 0, 'type' => 2, 'is_delete' => 0])
                        ->orderBy('create_time','desc')
                        ->get()
                        ->toArray();
        $allMessages = array_map('get_object_vars', $systemMessages);
        return DRS::returnOk($allMessages);
    }
}